﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TLTuan.App.TransferObj;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace TLTuan.App.AccessLayer
{
    public class CDAOThanhVien
    {
        #region Interface ...

        /// <summary>
        /// Thêm đối tượng thành viên
        /// </summary>
        /// <param name="ip_obj">Đối tượng cần thêm</param>
        /// <returns>True: nếu thành công
        /// False: nếu thất bại
        /// </returns>
        public bool Insert(CThanhVien ip_obj)
        {
            return (CConnection.ExecuteNonQuery("[pr_tblThanhvien_Insert]",
                            "@PK_ThanhvienID", ip_obj.PK_ThanhVienID,
                            "@Taikhoan", ip_obj.TaiKhoan,
                            "@Password", ip_obj.Password,
                            "@Tenthanhvien", ip_obj.TenThanhVien,
                            "@Ngaysinh", ip_obj.NgaySinh,
                            "@Gioitinh", ip_obj.GioiTinh,
                            "@Mobile", ip_obj.Mobile,
                            "@Email", ip_obj.Email,
                            "@Diem", ip_obj.Diem,
                            "@Ngaythamgia", ip_obj.NgayThamGia,
                            "@FK_QuyenID", ip_obj.FK_NhomID) > 0);
        }

        /// <summary>
        /// Update thông tin thành viên
        /// </summary>
        /// <param name="ip_obj"></param>
        /// <returns></returns>
        public bool Update(CThanhVien ip_obj)
        {
            return (CConnection.ExecuteNonQuery("[pr_tblThanhvien_Update]",
                "@PK_ThanhvienID", ip_obj.PK_ThanhVienID,
                "@Taikhoan", ip_obj.TaiKhoan,
                "@Password", ip_obj.Password,
                "@Tenthanhvien", ip_obj.TenThanhVien,
                "@Ngaysinh", ip_obj.NgaySinh,
                "@Gioitinh", ip_obj.GioiTinh,
                "@Mobile", ip_obj.Mobile,
                "@Email", ip_obj.Email,
                "@Diem", ip_obj.Diem,
                "@Ngaythamgia", ip_obj.NgayThamGia,
                "@FK_NhomID", ip_obj.FK_NhomID) > 0);
        }

        /// <summary>
        /// Delete một thành viên
        /// </summary>
        /// <param name="ip_Id"></param>
        /// <returns></returns>
        public bool Delete(int ip_Id)
        {
            SqlConnection v_conn = CConnection.GetConnection();

            SqlCommand v_comm = new SqlCommand("[pr_tblThanhvien_Delete]", v_conn);
            v_comm.CommandType = CommandType.StoredProcedure;
            v_comm.Parameters.Add("@PK_ThanhvienID", SqlDbType.Int).Value = ip_Id;
            int v_row = v_comm.ExecuteNonQuery();

            // Close connection
            CConnection.Close();

            if (v_row > -1)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Select tất cả các thành viên lên
        /// </summary>
        /// <returns></returns>
        public DataTable SelectAll2Dt()
        {
            return CConnection.GetData("pr_tblThanhvien_SelectAll");
        }

        public List<CThanhVien> SelectAll2Obj()
        {
            DataTable v_dt = this.SelectAll2Dt();
            if (v_dt == null || v_dt.Rows.Count == 0)
                return null;
            else
            {
                List<CThanhVien> op_lst = new List<CThanhVien>();
                foreach (DataRow item in v_dt.Rows)
                {
                    op_lst.Add(convertDataRow2Obj(item));
                }

                return op_lst;
            }
        }

        /// <summary>
        /// Select một thành viên lên
        /// </summary>
        /// <param name="ip_id">Id thành viên cần select</param>
        /// <returns>Đối tượng thành viên</returns>
        public CThanhVien SelectOne(int ip_id)
        {
            DataTable v_dt = CConnection.GetData("[pr_tblThanhvien_SelectOne]", 
                "@PK_ThanhvienID", ip_id);
            if (v_dt == null || v_dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                DataRow v_dr = v_dt.Rows[0];
                return convertDataRow2Obj(v_dr);
            }
        }

        public DataTable LoginSuccess(string ip_account, string ip_password)
        {
            DataTable v_dt = CConnection.GetData("pr_tblThanhvien_login",
                "@Account", ip_account, "@Password", ip_password);
            return v_dt;
        }


        public bool CheckRole(int ip_str_UserName)
        {
            //DataTable v_dt_result = new DataTable();
            //v_dt_result = CConnection.GetData("pr_tblUser_Get_Role_From_Username", "@UserName", ip_str_UserName);
            //if (int.Parse(v_dt_result.Rows[0][tblUser.Role].ToString()) == 2)
            //    return false;
            return true;
        }

        #endregion

        #region Method ...

        private CThanhVien convertDataRow2Obj(DataRow ip_dr)
        {
            // Khoi tao
            CThanhVien op_thanhvien = new CThanhVien();

            // Gan cac thuoc tinh
            op_thanhvien.PK_ThanhVienID = int.Parse(ip_dr["PK_ThanhvienID"].ToString());
            op_thanhvien.TaiKhoan = ip_dr["Taikhoan"].ToString();
            op_thanhvien.Password = ip_dr["Password"].ToString();
            op_thanhvien.TenThanhVien = ip_dr["Tenthanhvien"].ToString();
            op_thanhvien.NgaySinh = (DateTime)ip_dr["Ngaysinh"];
            op_thanhvien.GioiTinh = (bool)ip_dr["Gioitinh"];
            op_thanhvien.Mobile = ip_dr["Mobile"].ToString();
            op_thanhvien.Email = ip_dr["Email"].ToString();
            op_thanhvien.Diem = (ip_dr["Diem"] == null) ?
                0 : int.Parse(ip_dr["Diem"].ToString());
            op_thanhvien.NgayThamGia = (DateTime)ip_dr["Ngaythamgia"];
            op_thanhvien.FK_NhomID = (ip_dr["FK_NhomID"] == null) ?
                0 : int.Parse(ip_dr["FK_NhomID"].ToString());

            return op_thanhvien;
        }

        #endregion
    }
}
